package com.simple.relaxed.manager.impl;

import com.simple.relaxed.anno.Mark;
import com.simple.relaxed.anno.Slice;
import com.simple.relaxed.exception.AnnotationAbsenceException;
import com.simple.relaxed.manager.AbstractManager;
import lombok.Getter;
import ohos.aafwk.ability.AbilitySlice;

import java.util.Stack;

/**
 * 此管理这种，管理者所有已经触发的PageAbility的回退栈（非系统维护的回退栈）
 *
 */
@Mark(methodName = "getInstance")
public class SliceModeManager extends AbstractManager<String, Stack<AbilitySlice>> {

    public static final int STACK_MODE_BACK = 0x11;

    @Getter
    private int stackMode;

    private static SliceModeManager sliceModeManager = new SliceModeManager();

    private SliceModeManager() {
        super();
    }


    public static SliceModeManager getInstance() {
        if (sliceModeManager == null) {
            sliceModeManager = new SliceModeManager();
        }
        return sliceModeManager;
    }

    public void init(int stackMode) {
        this.stackMode = stackMode;
    }

    @Override
    public void register(String s, Stack<AbilitySlice> baseAbilitySlices) {
        throw new UnsupportedOperationException("不支持的操作");
    }

    @Override
    public void unregister(String s) {
        throw new UnsupportedOperationException("不支持的操作");
    }

    @Override
    public Stack<AbilitySlice> lookup(String s) {
        throw new UnsupportedOperationException("不支持的操作");
    }

    // 将AbilitySlice推入栈首
    public AbilitySlice pushAbilitySlice(AbilitySlice baseAbilitySlice) {
        Class<? extends AbilitySlice> aClass = baseAbilitySlice.getClass();
        Slice slice = aClass.getDeclaredAnnotation(Slice.class);
        if (slice == null) {
            throw new AnnotationAbsenceException("找不到注解".concat(Slice.class.getName()));
        }
        String bindName = slice.bindName();
        Stack<AbilitySlice> sliceStack = map.computeIfAbsent(bindName, k -> new Stack<>());
        sliceStack.push(baseAbilitySlice);
        return baseAbilitySlice;
    }

    // 将AbilitySlice从栈首弹出
    public AbilitySlice pop(String bindName) {
        Stack<AbilitySlice> sliceStack = map.get(bindName);
        if (sliceStack == null) {
            return null;
        }
        sliceStack.pop();
        if (sliceStack.isEmpty()) {
            return null;
        }
        return sliceStack.peek();
    }

    // 回退至指定AbilitySlice
    public AbilitySlice backTo(Class<? extends AbilitySlice> clazz) {
        Slice slice = clazz.getDeclaredAnnotation(Slice.class);
        if (slice == null) {
            throw new AnnotationAbsenceException("找不到注解".concat(Slice.class.getName()));
        }
        Stack<AbilitySlice> sliceStack = map.get(slice.bindName());
        if (sliceStack.isEmpty()) {
            return null;
        }
        boolean hasAbilitySlice = false;
        for (AbilitySlice baseAbilitySlice : sliceStack) {
            if (clazz.isInstance(baseAbilitySlice)) {
                hasAbilitySlice = true;
                break;
            }
        }
        if (hasAbilitySlice) {
            while (!sliceStack.isEmpty()) {
                AbilitySlice peek = sliceStack.peek();
                if (clazz.isInstance(peek)) {
                    return peek;
                }
                sliceStack.pop();
            }
        }
        return null;
    }

    // 查看栈顶的AbilitySlice
    public AbilitySlice lookupTop(String bindName) {
        Stack<AbilitySlice> sliceStack = map.get(bindName);
        if (sliceStack == null || sliceStack.isEmpty()) {
            return null;
        }
        return sliceStack.peek();
    }
}
